<script setup lang="ts">
import { ref, computed } from "vue";
import { Field, Calendar } from "vant";

interface PorpsData {
  modelValue?: [string, string]; // 双向绑定值
  title?: string; // 日历标题
  showTitle?: boolean; //是否展示标题
}

const propsData = withDefaults(defineProps<PorpsData>(), {
  modelValue: () => ["", ""],
});
const emits = defineEmits<{
  (e: "update:modelValue", val: string[]): void;
}>();

// 是否展示弹出层
const showPicker = ref(false);

// 日期格式转字符串
const format = (date: Date) =>
  `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;

const start = computed(() => {
  // 文本展示值
  return propsData.modelValue[0];
});

// 文本展示值
const end = computed(() => {
  return propsData.modelValue[1];
});

// 弹窗展示
const showPopup = () => {
  showPicker.value = true;
};

// 确定选择
const onConfirm = (selectedDate: any) => {
  const s = format(selectedDate[0]);
  const e = format(selectedDate[1]);
  showPicker.value = false;
  emits("update:modelValue", [s, e]);
};

// 重置日期
const resetDate = () => {
  emits("update:modelValue", ["", ""]);
};

// 是否显示重置按钮
const showReset = computed(() => {
  // 展示重置按钮
  if (propsData.modelValue[0] !== "" && propsData.modelValue[1] !== "") {
    return true;
  }
  return false;
});

// 活动样式
const activeClass = computed(() => {
  // 活动样式
  return showPicker.value ? "active" : "";
});

// 是否展示标题
const showTitle = computed(() => {
  if (propsData.title) {
    return true;
  }
  return false;
});

// 默认选中的日期
const defaultArr = computed(() => {
  const s = propsData.modelValue[0];
  const e = propsData.modelValue[1];
  if (s === "" && e === "") {
    console.log("空值");

    return null;
  } else {
    // return [new Date("2023/6/7"), new Date("2023/6/10")];
    const a = "2023/6/7",
      b = "2023/6/10";
    console.log(s);
    console.log(e);

    return [new Date(s), new Date(e)];
  }
});
</script>
<template>
  <div class="time-box">
    <field
      @click="showPopup"
      v-model="start"
      clearable
      placeholder="请选择"
      right-icon="notes-o"
      :class="activeClass"
      readonly
    />
    <span class="connect"></span>
    <field
      @click="showPopup"
      clearable
      v-model="end"
      placeholder="请选择"
      right-icon="notes-o"
      :class="activeClass"
      readonly
    />
    <div class="public-blue-font" v-if="showReset" @click="resetDate">重置</div>
  </div>

  <calendar
    v-model:show="showPicker"
    type="range"
    :show-title="showTitle"
    :title="propsData.title"
    :default-date="defaultArr"
    @confirm="onConfirm"
  />
</template>

<style lang="scss" scoped>
.time-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // padding: 20px;
  :deep(.van-field) {
    flex-wrap: wrap;
    border: 1px solid #eeeeee;
    // height: 32px;
    padding: 5px 10px;
    border-radius: 4px;
  }
  .active {
    border: 1px solid #1677ff;
    :deep(.van-icon) {
      color: #1677ff;
    }
  }
  .connect {
    display: inline-block;
    width: 12px;
    height: 1px;
    background-color: #333333;
    margin: 0px 5px;
    // width: calc(50% - 10px);
  }
  .public-blue-font {
    color: #1677ff;
    margin-left: 5px;
    min-width: 36px;
    font-size: 14px;
  }
}
.title {
  color: #666666;
  font-size: 16px;
  position: relative;
  display: flex;
  padding: 10px 0;
  align-items: center;
  padding-left: 15px;
  .con {
    flex: 1;
    text-align: right;
    margin-right: 10px;
  }
  .icons {
    color: #d4d4d4;
    // margin-top: 4px;
  }
}
</style>
